package xrz.com.jr;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

import myhttps.Http;
import myhttps.HttpConst;
import myhttps.HttpListener;
import xrz.com.jr.adapter.Adapter_XiTong;
import xrz.com.jr.entity.XueXi;
import xrz.com.jr.utils.BaseDialog;
import xrz.com.jr.utils.ExitUtil;

public class XitongXueXiActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView mListview;
    private List<XueXi> datas = new ArrayList<XueXi>();
    private Adapter_XiTong adapter;
    private TextView Tv_Title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_base);
        init();
        ExitUtil.getInstant().addToPool(this);
    }

    private void init() {
        mListview = (ListView) findViewById(R.id.listview);
        Tv_Title = (TextView) findViewById(R.id.top_tv);
        Tv_Title.setText("系统学习");
        adapter = new Adapter_XiTong(this);
        mListview.setAdapter(adapter);
        mListview.setOnItemClickListener(this);
        Http.getInstant().sendRequest(this, HttpConst.getXiTongXueXi, null, new HttpListener() {
            @Override
            public void onHttpSuccess(String result) {
                datas = JSON.parseArray(result, XueXi.class);
                adapter.addAll(datas);
            }

            @Override
            public void onHttpError(String error) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(XitongXueXiActivity.this, XiTongContentActivity.class);
        intent.putExtra("data", datas.get(i).getContent());
        intent.putExtra("type", "2");
        startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            BaseDialog.showDialog(XitongXueXiActivity.this, new BaseDialog.onDialogBtnClickListener() {
                @Override
                public void onClick() {
                    ExitUtil.getInstant().exitApp(XitongXueXiActivity.this);
                }
            });
        }
        return super.onKeyDown(keyCode, event);
    }
}
